package cate.game.activity.newhero.data;

import cate.common.table.activity.dailylogin.item.DailyLoginActivityItemSheet;
import cate.common.table.activity.heroup.sign.HeroUpSignRow;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.newhero.NewHeroActivity;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

public class NewHeroSignLevel extends SubActivitySingle<NewHeroActivity> {
    @NoteField(value = "档位")
    public int level;
    @NoteField(value = "登录天数")
    public int loginDay;
    @NoteField(value = "上次检测过天时间")
    public long lastLoginCheckTime;
    @NoteField(value = "领取状态")
    public Map<Integer, Boolean> gotMap;

    public NewHeroSignLevel() {
    }

    public NewHeroSignLevel(int level) {
        this.level = level;
    }

    @Override
    public void initialize(NewHeroActivity parent) {
        super.initialize(parent);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
    }

    public void excelCheck() {
        Map<Integer, HeroUpSignRow> items = showItems();
        if (items == null) {
            gotMap.clear();
            return;
        }
        gotMap.entrySet().removeIf((entry) -> !items.containsKey(entry.getKey()));
        for (int itemTid : items.keySet()) {
            if (!gotMap.containsKey(itemTid)) {
                gotMap.put(itemTid, false);
            }
        }
    }

    public void clear() {
        loginDay = 0;
        lastLoginCheckTime = 0;
        gotMap.clear();
    }

    @Override
    public void onEnter() {
        super.onEnter();
        long nowTime = game().time.now();
        if (TimeTool.isDifferentDay(lastLoginCheckTime, nowTime)) {
            loginDay = parent.getCurrentDay();
            lastLoginCheckTime = nowTime;
        }
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        long nowTime = game().time.now();
        if (silence) {
            if (!TimeTool.isDifferentDay(lastLoginCheckTime, nowTime)) {
                return;
            }
        }
        loginDay = parent.getCurrentDay();
        lastLoginCheckTime = nowTime;
    }

    /**
     * 获取指定奖励配置
     *
     * @param itemTid 奖励项ID
     */
    public HeroUpSignRow showItemConfig(int itemTid) {
        return parent.showItemConfig(itemTid);
    }


    /**
     * 获取当前档位奖励配置MAP
     */
    public Map<Integer, HeroUpSignRow> showItems() {
        Map<Integer, HeroUpSignRow> items = new HashMap<>();
        switch (funcId()) {
            case GDFunc.NEW_HERO: {
                role.getGame().table.heroUp.sign.getList().forEach(config -> {
                    if (config.configTid == parent.configTid() && config.level == level) {
                        items.put(config.id, config);
                    }
                });
            }
            break;
        }
        return items;
    }

    /**
     * 签到奖励
     *
     * @param itemTid 配置ID
     * @return 领取结果 无携带数据
     */
    public GameResult<NewHeroActivity> signReward(int itemTid) {
        GameResult<NewHeroActivity> r = new GameResult<>();
        r.data = parent;
        if (!enable()) {
            return r.fail("未开启");
        }
        HeroUpSignRow config = showItemConfig(itemTid);
        if (config == null || !gotMap.containsKey(itemTid)) {
            return r.fail("找不到配置");
        }
        if (gotMap.get(itemTid)) {
            return r.fail("已领取该奖励");
        }
        if (loginDay < config.day) {
            return r.fail("登录天数不足");
        }
        if (config.getType == DailyLoginActivityItemSheet.DAILY_LOGIN_ACT_GET_TYPE_PAY) {
            return r.fail("未充值不可领取");
        }
        provideReward(itemTid);
        return r.success();
    }

    /**
     * 是否允许购买
     *
     * @param itemTid 配置ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> allowPay(int itemTid) {
        GameResult<Void> r = new GameResult<>();
        if (!enable()) {
            return r.fail("未开启");
        }
        HeroUpSignRow config = showItemConfig(itemTid);
        if (config == null || !gotMap.containsKey(itemTid)) {
            return r.fail("找不到配置");
        }
        if (gotMap.get(itemTid)) {
            return r.fail("已领取该奖励");
        }
        if (loginDay < config.day) {
            return r.fail("登录天数不足");
        }
        if (config.getType != DailyLoginActivityItemSheet.DAILY_LOGIN_ACT_GET_TYPE_PAY) {
            return r.fail("不用充值你充你MUA呢？");
        }
        return r.success();
    }

    /**
     * 签到追加奖励
     *
     * @param itemTid 配置ID
     */
    public void provideReward(int itemTid) {
        HeroUpSignRow config = showItemConfig(itemTid);
        if (config == null) {
            return;
        }
        MixRes rewardRes = new MixRes(config.rewardStr);
        if (rewardRes.isEmpty()) {
            return;
        }
        EcResult exe = new MixResAdder().setRes(rewardRes)
                .setOperation(GDOperation.ACT_HERO_UP_SIGN_REWARD)
                .setOperationSub(itemTid)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        gotMap.put(itemTid, true);
    }
}
